#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>

#define H 25
#define W 50

int arr[H][W];

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

void startup()
{
	int i, j;
	for (i = 0; i < H; i++)
		for (j = 0; j < W; j++)
			arr[i][j] = 1;
}

void show()
{
	gotoxy(0, 0);
	int i, j;
	for (i = 0; i <= H; i++)
	{
		for (j = 0; j <= W; j++)
		{
			if (arr[i][j] == 1)
				printf("*");
			else
			{
				printf(" ");
			}
		}
		printf("\n");
	}
	Sleep(50);
}

void WI()
{
	int lles[H][W];
	int bour;
	int i, j;
	for (i = 1; i <= H - 1; i++)
	{
		for (j = 1; j <= W - 1; j++)
		{
			bour = arr[i - 1][j - 1] + arr[i - 1][j] + arr[i - 1][j + 1] + arr[i][j - 1] + arr[i][j + 1]
				+ arr[i + 1][j - 1] + arr[i + 1][j] + arr[i + 1][j + 1];
			if (bour == 3)
				lles[i][j] = 1;
			else if (bour == 2)
				lles[i][j] = arr[i][j];
			else
				lles[i][j] = 0;
		}
	}
	for (i = 1; i <= H - 1; i++)
		for (j = 1; j <= W - 1; j++)
			arr[i][j] = lles[i][j];
}

void OU()
{

}

void hj6()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

int main()
{
	startup();
	while (1)
	{
		show();
		WI();
		OU();
		hj6();
	}
	return 0;
}